package com.lwq.project.controller;

import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lwq.project.activemq.ConsumerService;
import com.lwq.project.activemq.ProducerService;
import com.lwq.project.redis.RedisDao;
import com.lwq.project.service.Role2muneService;
import com.lwq.project.service.User2roleService;
import com.lwq.project.service.UserService;

@Controller
public class IndexController {
	@Autowired
	private UserService userService;
	@Autowired
	private User2roleService user2roleService;
	@Autowired
	private Role2muneService role2muneService;
	@Autowired
	private ProducerService producerService;
    @Autowired
    private RedisDao redisDao;

    @Autowired
    ConsumerService consumerService;
    
    
	@RequestMapping("/login.action")
	public String loginView(HttpServletRequest rq,Model model){
		return "login/login";
	}
	
	@RequestMapping("/loginC.action")
	public String loginCheck(HttpServletRequest rq,String adminName,String adminPwd,Model model){
		String uId = userService.getIdByNameAndPwd(adminName,adminPwd);
		String msg="登陆成功";
		ActiveMQQueue amqq = new ActiveMQQueue();
		redisDao.set("user", "123");
		if(!StringUtils.isBlank(uId)){
			String rId = user2roleService.getRoleIdByUserId(uId);
			HttpSession session =rq.getSession();
			session.setAttribute("uPrimary", uId);
			session.setAttribute("rPrimary", rId);
			session.setAttribute("uName", adminName);
			return  "redirect:/index.action";
		}else{
			model.addAttribute("error", "用户名或密码错误！");
			return "login/login";
		}
		
	}
	
	@RequestMapping("/index.action")
	public String toIndex1(HttpServletRequest rq,Model model,HttpSession session){
		String rId = (String) session.getAttribute("rPrimary");
		List<Map<String,Object>> list = role2muneService.getMune(rId);
		model.addAttribute("muneList", list);
		return "/main/index";
	}
	
	@RequestMapping("/*")
	public String error(){
		return "login/login";
	}
	
	@RequestMapping("/cancel")
	public String cancel(HttpServletRequest request){
		Enumeration em = request.getSession().getAttributeNames();
		while(em.hasMoreElements()){
			request.getSession().removeAttribute(em.nextElement().toString());
		}
		return "login/login";
	}
	@RequestMapping(value="/sendMessage.action",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String sendMessage(String topic, String msg){
		producerService.sendMessageTopic(topic,redisDao.get("user") + ":" +msg);
		return "success";
	}
}
